<?php
//模块插件类
//插件功能类，由系统调用，实现具体功能。
defined('BY_JYA') or exit('error');
class core_jy_sns extends plugin{

}
function checkMember()
		{
			
			if (!empty($_SESSION['uid'])) {
				$member = pdo_get('jy_sns_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'])); 

				if (empty($member)) {
					$member = array('uniacid' => $_SESSION['uniacid'], 'mid' => $_SESSION['openid'], 'createtime' => time());
					pdo_insert('jy_sns_member', $member);
				}
				else {
					if (!empty($member['isblack'])) {
						exi('禁止访问，请联系客服!','error','/index/');
					}
				}
			}
		}

function getMember($mid)
		{
			
			$member = pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$mid));
			$sns_member = pdo_get('jy_sns_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));

			if (empty($sns_member)) {
				$member['sns_credit'] = $member['credit1'];;
				$member['sns_level'] = 0;
				$member['notupgrade'] = 0;
			}
			else {
				$member['mid'] = $sns_member['mid'];
				$member['sns_id'] = $sns_member['id'];
				$member['sns_credit'] = $member['credit1'];
				$member['sns_level'] = $sns_member['level'];
				$member['sns_sign'] = $sns_member['sign'];
				$member['sns_notupgrade'] = $sns_member['notupgrade'];
			}

			return $member;
		}
function isSuperManager($mid = ''){		
			if (empty($mid)) {
				$mid = $_SESSION['uid'];
			}
			$set =json_decode(pdo_getcolumn('core_account_plugin_setting',array('uniacid'=>$_SESSION['uniacid'],'plugin'=>'jy_sns','key'=>'setting'),'data'),true);
			if($mid==$set['managers'] && $set['managers']){
				return true;
			}else{
				return false;
			}
			
}
function isManager($bid = 0, $mid = ''){	
			if (empty($mid) && $mid!=0) {
				$mid = $_SESSION['uid'];
			}
			$count = pdo_count('jy_sns_manage',array('uniacid'=>$_SESSION['uniacid'],'bid'=>$bid,'mid'=>$mid)); 
			return 0 < $count;
}
function check($member, $board, $isPost = false)
		{
			
			$levelid = $member['level'];
			$groupid = $member['groupid'];
			$levels = ($isPost ? $board['postlevels'] : $board['showlevels']);

			if ($levels != '') {
				$arr = explode(',', $levels);

				if (!in_array($levelid, $arr)) {
					

					exi('您的会员等级没有权限浏览此版块','error','/index/');
				}
			}

			$levels = ($isPost ? $board['postgroups'] : $board['showgroups']);

			if ($levels != '') {
				$arr = explode(',', $levels);

				if (!in_array($groupid, $arr)) {
					

					exi('您的会员组没有权限浏览此版块','error','/index/');
				}
			}

			$levels = ($isPost ? $board['postsnslevels'] : $board['showsnslevels']);

			if ($levels) {
				$arr = explode(',', $levels);

				if (!in_array($member['sns_level'], $arr)) {
					

					exi('您的社区等级没有权限浏览此版块','error','/index/');
				}
			}
			return true;
}
function getPostCount($bid){
	return pdo_count('jy_sns_post',array('uniacid'=>$_SESSION['uniacid'],'bid'=>$bid,'pid'=>0,'deleted'=>0));
}
function getFollowCount($bid){
		return pdo_count('jy_sns_board_follow',array('uniacid'=>$_SESSION['uniacid'],'bid'=>$bid));
}
function isFollow($bid)
		{
			$count = pdo_get('jy_sns_board_follow',array('uniacid'=>$_SESSION['uniacid'],'bid'=>$bid,'mid'=>$_SESSION['uid']));
			if($count){
				return 1;
			}else{
				return 0;
			}
			
		}
/**
         * 获取置顶帖
         * @param int $bid
         */
 function getTops($bid = 0){
			if (!empty($bid)) {
				$condition=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'pid'=>0,'isboardtop'=>1);
			}else{
				$condition=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'pid'=>0,'istop'=>1);
			}
			return pdo_getall('jy_sns_post',$condition); 
}
function getPost($pid = '0'){
			return pdo_get('jy_sns_post',array('uniacid'=>$_SESSION['uniacid'],'id'=>$pid));
}
 function getAvatar($avatar = ''){
			if (empty($avatar)) {
				$set =json_decode(pdo_getcolumn('core_account_plugin_setting',array('uniacid'=>$_SESSION['uniacid'],'plugin'=>'jy_sns','key'=>'setting'),'data'),true);
				$avatar = (empty($set['head']) ? '../addons/jy_sns/images/head.jpg' : tomedia($set['head']));
			}

			return $avatar;
}
function getBoard($bid = '0'){
			
			return pdo_get('jy_sns_board',array('id'=>$bid,'uniacid'=>$_SESSION['uniacid']));
}
function replaceContent($content){
			return str_replace("\n", '<br/>', preg_replace('/\\[EM(\\w+)\\]/', '<img src="../addons/jy_sns/images/face/1.gif" class="emoji" />', $content));
}
function timeBefore($the_time){
			$now_time = time();
			$dur = $now_time - $the_time;

			if ($dur < 0) {
				return $the_time;
			}

			if ($dur < 60) {
				return '刚刚';
			}

			if ($dur < 3600) {
				return floor($dur / 60) . '分钟前';
			}

			if ($dur < 86400) {
				return floor($dur / 3600) . '小时前';
			}

			if ($dur < 259200) {
				return floor($dur / 86400) . '天前';
			}

			return date('m-d', $the_time);
}
?>